package graphBuilder;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import Shared.Utils;
import Simulator.GUI;


public class graphBuilder extends JPanel
{
	private String londra = "../gui/londra.jpg";
	//private AmbulanceGraphic ambulance;
	private Image mybackground;
	public ArrayList<Point[]> list;
	private String punaise = "../gui/pin.png";
	private Image mypin;
	private String bin_file_name = "thoralf.bin";

	public graphBuilder() 
	{
		Utils.disp("Loading "+bin_file_name+" ...");


		//list = new ArrayList<Point>();

		ObjectInputStream in;
		try 
		{

			in = new ObjectInputStream(new FileInputStream(new File(bin_file_name)));
			list = (ArrayList<Point[]>) in.readObject();
			//correct();
			Utils.disp("Loaded point.bin");
		} 
		catch (FileNotFoundException e) 
		{
			list = new ArrayList<Point[]>();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}


		ImageIcon pin = new ImageIcon(this.getClass().getResource(punaise));
		mypin = pin.getImage();


		ImageIcon back = new ImageIcon(this.getClass().getResource(londra));
		mybackground = back.getImage();

		addKeyListener(new TAdapter());
		addMouseListener(new TAdapter());
		setFocusable(true);
		setBackground(Color.BLACK);
		setDoubleBuffered(true);

	}


	public void paint(Graphics g) 
	{
		super.paint(g);

		Graphics2D g2d = (Graphics2D)g;
		g2d.drawImage(mybackground, 0, 0, null); 
		//g2d.drawImage(ambulance.getImage(), ambulance.getX(), ambulance.getY(), this);

		Stroke s = g2d.getStroke();
		g2d.setStroke(new BasicStroke(3));

		for(Point[] p : list)
		{
			g2d.drawLine((int) p[0].getX() , (int) p[0].getY(), (int) p[1].getX(), (int) p[1].getY());

			g2d.drawImage(mypin,((int) p[0].getX() - 2),((int) p[0].getY() -2), this); 
			g2d.drawImage(mypin,((int) p[1].getX() - 2),((int) p[1].getY() -2), this); 
		}

		g2d.setStroke(s);

		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}


	private class TAdapter extends KeyAdapter implements MouseListener
	{
		private Point waitPoint = null;

		public void keyReleased(KeyEvent e) 
		{
			Utils.disp("key rel");
		}

		public void keyPressed(KeyEvent e) 
		{
			Utils.disp("key press");
			if(e.getKeyCode() == KeyEvent.VK_ENTER)
			{
				Utils.disp("Saving "+bin_file_name+" ...");

				ObjectOutputStream out;
				try 
				{

					out = new ObjectOutputStream(new FileOutputStream(new File(bin_file_name)));
					out.writeObject(list);
					Utils.disp("Saved point.bin");
				} 
				catch (FileNotFoundException e1) 
				{
					e1.printStackTrace();
				} catch (IOException e1) 
				{
					e1.printStackTrace();
				}
			}
			else if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
			{
				Utils.disp("CTRL-Z");
				list.remove(list.size()-1);// rem last op
				repaint();
			}

		}

		@Override
		public void mouseClicked(MouseEvent arg0) 
		{
			if(waitPoint==null)
			{
				waitPoint= arg0.getPoint();
				Utils.disp("Stored = "+waitPoint.toString());
			}
			else
			{
				Point [] newLine = {waitPoint,arg0.getPoint()};
				list.add(newLine);
				waitPoint = null;
				repaint();  
				Utils.disp(newLine[0].toString() + ","+ newLine[1].toString());

			}


		}

		@Override
		public void mouseEntered(MouseEvent arg0) {}

		@Override
		public void mouseExited(MouseEvent arg0) {}

		@Override
		public void mousePressed(MouseEvent arg0) {}

		@Override
		public void mouseReleased(MouseEvent arg0) {}
	}


	public static void main(String[] args) 
	{
		new GUI(new graphBuilder());
	}
}